var AuditManager = function() {

	// Form表单提交信息
	var fnEditCallback = function(data) {
		$("#saveBtn").button("reset");
		if (data.type == 'success') {
			BootstrapDialog.show({
				title : '提示',
				message : '数据保存成功',
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
						window.location.href = ctx + '/letterInfo/list.html';
					}
				} ]
			});
		} else {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : '警告',
				message : data.message,
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ]
			});
		}
	};

	var fnRejectCallback = function(data) {
		$("#rejectBtn").button("reset");
		if (data.type == 'success') {
			BootstrapDialog.show({
				title : '提示',
				message : '数据保存成功',
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
						window.location.href = ctx + '/letterInfo/list.html';
					}
				} ]
			});
		} else {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : '警告',
				message : data.message,
				buttons : [ {
					label : '确定',
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ]
			});
		}
	};
	
	return {
		/**
		 * 初始状态
		 */
		init : function() {
			/*$('#expressTime').datetimepicker({
        		lang:'ch',
        		timepicker:false,
        		format:'Y-m-d'
        	});*/
			this.initListener();
			this.initExpressCodeList();
		},

		/**
		 * 监听开关事件
		 */
		initListener : function() {
			$("#saveBtn").click(function() {
				var form1 = $('#addOrEditForm');
				form1.validate({
					// 验证规则
					rules : {
						expressNo : {
							required : true
						},
						expressCompany : {
							required : true
						},
						expressTime : {
							required : true
						}
					},
					messages : {
						expressNo : {
							required : "请输入快递单号"
						},
						expressCompany : {
							required : "请选择快递公司"
						},
						expressTime : {
							required : "请选择派件时间"
						}
					},
				});
				if (form1.valid()) {
					var postData = $("#addOrEditForm").serializeJson();
					postData['letterInfoState'] = 1;
					$.ajax({
						"dataType" : 'json', // 接受数据格式
						"type" : "POST", // 数据发送方式
						"data" : postData,
						"url" : ctx + "/letterInfo/save", // 后台处理程序
						"success" : fnEditCallback
					});
				}
			});
			
			/**
			 * 审核驳回
			 */
			$("#rejectBtn").click(function() {
				var postData = $("#addOrEditForm").serializeJson();
				postData['letterInfoState'] = 2;
				$.ajax({
					"dataType" : 'json', // 接受数据格式
					"type" : "POST", // 数据发送方式
					"data" : postData,
					"url" : ctx + "/letterInfo/save", // 后台处理程序
					"success" : fnRejectCallback
				});
			});
			
			/**
			 * 返回
			 */
			$("#cancel").click(function() {
				var letterInfoState = $("#letterInfoState").val();
				if(letterInfoState == 0){
					window.location.href = ctx + '/letterInfo/list.html';
				}else if(letterInfoState == 1){
					window.location.href = ctx + '/letterInfo/sentList.html';
				}else if(letterInfoState == 2){
					window.location.href = ctx + '/letterInfo/rejectList.html';
				}
			});

		},
		
		/**
		 * 加载快递公司列表
		 */
		initExpressCodeList : function() {
            var expressCompany = "请选择";
            $.ajax({
                type : "GET",
                url : ctx + "/expressCode/getExpressCodeList",
                contentType : 'application/json;charset=utf-8',
                dataType : 'json',
                async : false,
                success : function(result) {
                    $('#expressCompany').empty();
                    $("#expressCompany").append("<option value=''>请选择</option>");
                    $.each(result, function (i, item) {
                        $("#expressCompany").append('<option value=' + item.expressCodeId+ '>' + item.name  +'</option>');
                    });
                }
            });
            $("#expressCompany").find("span").empty();
            $("#expressCompany").find("span").html(expressCompany);
        }
		
	};
}();

/*
 * Powered By [Code-Generator]
 * Web Site: http://www.solveanalysi.tech
 */
